package com.wswy.wzcx.model;

import android.text.TextUtils;

import com.google.gson.annotations.SerializedName;
import com.wswy.wzcx.model.license.UserCarInfo;
import java.util.ArrayList;
import java.util.List;

public class OCRResult {

  //public String msg;
  private List<OCRItem> ocrItems;


  @SerializedName("carNo")
  public String carNo;

  @SerializedName("carType")
  public String carType;

  @SerializedName("frameNo")
  public String frameNo;

  @SerializedName("engineNo")
  public String engineNo;


  public boolean ocrSuccess(){
    extraCarNo();

    return !TextUtils.isEmpty(carNo);
  }


  public void setOcrItems(List<OCRItem> ocrItems) {
    this.ocrItems = new ArrayList<>();
    if(ocrItems != null){
      for (OCRItem ocrItem : ocrItems) {
        if(!TextUtils.isEmpty(ocrItem.value)){
          this.ocrItems.add(ocrItem);
        }
      }
    }
  }

  public List<OCRItem> getOcrItems() {
    return ocrItems;
  }


  private void extraCarNo(){
    if(TextUtils.isEmpty(carNo) && ocrItems != null) {
      for (OCRItem ocrItem : ocrItems) {
        if ("车牌号码".equals(ocrItem.item)) {
          if(ocrItem.value.length() >=2){
            carNo = ocrItem.value;
          }
          break;
        }
      }
    }
  }

  public String extraProvince(){
    extraCarNo();

    return carNo!= null?String.valueOf(carNo.charAt(0)):null;
  }

  public String extraAlphabet(){
    extraCarNo();
    return carNo!= null?String.valueOf(carNo.charAt(1)):null;
  }

  public CarInfoModel convert2CarInfo(){
    CarInfoModel model = new CarInfoModel();
    if(ocrItems != null){
      for (OCRItem ocrItem : ocrItems) {
        if("车牌号码".equals(ocrItem.item)){
          model.carNo=ocrItem.value;
        }else if ("发动机号".equals(ocrItem.item)){
          model.engineNo = ocrItem.value;
        }else if("识别代码".equals(ocrItem.item)){
          model.frameNo = ocrItem.value;
        }
      }
    }else {
      if (carNo != null){
        model.carNo=carNo;
        model.engineNo = engineNo;
        model.frameNo = frameNo;
        model.carType =carType;
      }

    }
    model.ocrHash = model.toOcrHash();
    return model;
  }


  @Override
  public String toString() {
    return "OCRResult{" +
            "ocrItems=" + ocrItems +
            ", carNo='" + carNo + '\'' +
            ", carType='" + carType + '\'' +
            ", frameNo='" + frameNo + '\'' +
            ", engineNo='" + engineNo + '\'' +
            '}';
  }
}
